package com.bawei.hgshop.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bawei.hgshop.entity.Brand;
import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.service.BrandService;
import com.bawei.hgshop.utils.HgFile;
import com.github.pagehelper.PageInfo;

@RequestMapping("brand")
@Controller
public class BrandController  {
	
	@Reference
	BrandService brandService;
	
	/**
	 * 列表
	 * @param request
	 * @param brandName
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest request,
			String brandName,
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int pageSize
			) {
		
		PageInfo<Brand> pageInfo = brandService.list(brandName,page,pageSize);
		System.out.println("pageInfo " + pageInfo);
		
		pageInfo.getList().forEach(x->{System.out.println("x is " + x);});
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("brandName", brandName); 
		
		
		return "brand/list";
	}
	
	/**
	 * 跳转到添加的页面
	 * @return
	 */
	@RequestMapping("toAdd")
	public String toAdd() {
		return "brand/add";
	}
	
	/**
	 * 接收提交的数据进行添加
	 */
	@RequestMapping("add")
	@ResponseBody
	public MsgResult add(HttpServletRequest request, Brand brand,MultipartFile file) {
		
		if(file==null || file.isEmpty()) {
			return new MsgResult(1,"图片不能为空",null);
			
		}
		
		String rootPath="d:/pic";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String subPath = format.format(new Date());
		// 获取文件应该存放的路径
		File filePath = new File(rootPath+"/"+ subPath);
		//判断路径是否存在
		if(!filePath.exists()) {
			//没有目录则创建路径
			filePath.mkdirs();
		}
		
		//上传图片
		// 随机生成一个文件名称
		String fileName = UUID.randomUUID().toString()+file.getOriginalFilename();
		
		// 文件另存
		try {
			file.transferTo(new File(rootPath+"/"+ subPath + "/" + fileName));
			// 实体Bean保存一下文件的路径
			brand.setPicPath(subPath + "/" + fileName);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int n = brandService.add(brand);
		//保存成功
		if(n>0)
			return new MsgResult(0, "", null);
		else
			return new MsgResult(2, "保存失败，请稍后再试", null);
	}
	
	/**
	 * 跳转到修改的页面
	 * @return
	 */
	@RequestMapping("toupdate")
	public String toUpdate(HttpServletRequest request,int id) {
		Brand brand = brandService.getById(id);
		request.setAttribute("brand", brand);
		return "brand/update";
	}
	
	
	/**
	 * 接收提交的数据进行修改
	 */
	@RequestMapping("update")
	@ResponseBody
	public MsgResult update(HttpServletRequest request, Brand brand,MultipartFile file) {
		

			
		String filePath = HgFile.upload(file);
		brand.setPicPath(filePath);
		
		int n = brandService.update(brand);
		//保存成功
		if(n>0)
			return new MsgResult(0, "", null);
		else
			return new MsgResult(2, "保存失败，请稍后再试", null);
	}
	
	
	/**
	 *  批量删除
	 * @return
	 */
	@RequestMapping("delbatch")
	@ResponseBody
	public MsgResult delBatch(HttpServletRequest request,@RequestParam(value = "ids[]") int[] ids) {
		int n  = brandService.delBatch(ids);
		//删除成功
		if(n>0)
			return new MsgResult(0, "", null);
		else
			return new MsgResult(2, "删除失败，请稍后再试", null);
	}
	

}
